/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: errors.cpp
 *
 * Purpose: Collie base error code define and helper API implementation.
 *
 * Developer:
 *   wen.gu , 2020-05-11
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "collie/core/errors.h"


namespace collie
{
namespace core
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define ERR_CASE(err) case err: ret = #err; break

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
const char* ErrorStr(CollieErrc errc)
{
    char const* ret = "unknown";
    switch (errc)
    {
    ERR_CASE(CollieErrc::OK);
    ERR_CASE(CollieErrc::Fail);
    ERR_CASE(CollieErrc::BadParameter);
    ERR_CASE(CollieErrc::NoInit);
    ERR_CASE(CollieErrc::Eof);
    ERR_CASE(CollieErrc::NotImplemented);
    ERR_CASE(CollieErrc::NotFound);
    ERR_CASE(CollieErrc::Undefined);
    ERR_CASE(CollieErrc::InvaildName);
    ERR_CASE(CollieErrc::InsufficientResources);
    ERR_CASE(CollieErrc::InvalidStatus);
    ERR_CASE(CollieErrc::OutOfRange);
    ERR_CASE(CollieErrc::UnsupportedType);
    ERR_CASE(CollieErrc::Timeout);
    ERR_CASE(CollieErrc::BadAddress);
    ERR_CASE(CollieErrc::ConnectionAborted); /**< connection aborted(ECONNABORTED) */
    ERR_CASE(CollieErrc::ConnectionAlreadyInProgress); /**< connection already in progress(EALREADY) */
    ERR_CASE(CollieErrc::ConnectionRefused); /**< connection refused(ECONNREFUSED) */
    ERR_CASE(CollieErrc::ConnectionReset); /**< connection reset(ECONNRESET) */
    ERR_CASE(CollieErrc::BadMessage);
    ERR_CASE(CollieErrc::OperationCanceled); /**< operation canceled(ECANCELED) */
    ERR_CASE(CollieErrc::OperationInProgress); /**< operation in progress(EINPROGRESS) */
    ERR_CASE(CollieErrc::OperationNotPermitted); /**< operation not permitted(EPERM) */
    ERR_CASE(CollieErrc::OperationNotSupported); /**< operation not supported(EOPNOTSUPP) */
    ERR_CASE(CollieErrc::OperationWouldBlock); /**< operation would block(EWOULDBLOCK) */
    ERR_CASE(CollieErrc::OutOfMemory);
    ERR_CASE(CollieErrc::AlreadyExisted);
    ERR_CASE(CollieErrc::ResourceIsBusy);
    default: break;
    }

    return ret;
}

#if 1
void Error2Json(CollieErrc err, Value& val)
{
    val["code"] = (int32_t)err;
    val["description"] = ErrorStr(err);
}

CollieErrc Json2Error(const Value& val)
{
    CollieErrc ret = CollieErrc::Undefined;

    if (val.isMember("code"))
    {
        ret = (CollieErrc)val["code"].asInt();
    }

    return ret;
}
#endif

} /** namespace core */
} /** namespace collie */


